/**Created by the LayaAirIDE*/
module view{
	export class GameSelect extends ui.GameSelectUI{
		constructor(){
			super();

			this._init();
		}

		_init(): void {
			this.soundBtn.on("click", this, this.soundBtnClick, [0])
			this.exitBtn.on("click", this, this.gameExit, [0])
			this.updateSoundBtn(!SoundPlayer.Instance.mute)

			//布局选项关卡
			var stages:Array<any> = [true, false, false, false, false, false, false, false, false, false, false]
			for (var index = 0; index < stages.length; index++) {
				var element: boolean = stages[index];
				
				var view: Laya.Button = new Laya.Button()
				view.stateNum = 1
				if (element) {
					view.skin = "images/item_level.png"
					view.label = (index + 1).toString()
					view.labelSize = 38
					view.labelColors = "#ffffff"
				}else {
					view.skin = "images/item_level_lock.png"
				}
				
				view.tag = index
				view.selected = element
				view.on("click", this, (button: Laya.Button) => {
					if (button.selected == false) {
						console.log("未解锁")
						return
					}
					game.gameMain(button.tag + 1)
				}, [view])
				view.pos(25 + 90*(Math.floor(index%7)), 100 + 90*(Math.floor(index/7)))
				this.levelContainer.addChild(view)
			}
		}

		soundBtnClick(): void {
			this.updateSoundBtn(!this.soundBtn.selected)
			SoundPlayer.Instance.setMute(!this.soundBtn.selected)
		}

		updateSoundBtn(selected: boolean): void {
			if (selected) {
				this.soundBtn.skin = "images/button_sound_off.png"
			}else {
				this.soundBtn.skin = "images/button_sound_on.png"
			}

			this.soundBtn.selected = selected
		}

		gameExit(): void {
			var exitDialog: view.GameExitDialog = new view.GameExitDialog()
			exitDialog.zOrder = 100
			
			this.addChild(exitDialog)
			exitDialog.zOrder = 10000

			exitDialog.confirmBtn.on("click", this, () => {
				game.gameExit()
				this.removeSelf()
				this.destroy()
				exitDialog.removeSelf()
				exitDialog.destroy()
			}, [0])

			exitDialog.cancelBtn.on("click", this, () => {
				exitDialog.removeSelf()
				exitDialog.destroy()
			}, [0])
		}
	}
}